/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hk.ces.oxfam.patch.config;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.log4j.Logger;

/**
 *
 * @author ReutyCheng
 */
public class Setting
{
  public interface Value<T>
  {
    T getValue(String key);
  };
  
  private static class SettingHolder
  {
    private static Setting instance = new Setting();
  }

  public static final String ENABLED_COMMIT = "ENABLED_COMMIT";
  
  private final Properties properties = new Properties();
  private final Map<String, Object> propertiesMap = new HashMap<String, Object>();
  public final Value<Boolean> booleanValue;

  private Setting()
  {
    try
    {
      this.properties.load(this.getClass().getResourceAsStream("/setting.properties"));
      this.booleanValue = new Value<Boolean>()
      {
        public Boolean getValue(String key)
        {
          if (propertiesMap.containsKey(key))
          {
            return (Boolean) propertiesMap.get(key);
          }
          else
          {
            boolean property = Boolean.valueOf(properties.getProperty("ENABLED_COMMIT"));
            propertiesMap.put(key, property);
            return property;
          }
        }
      };
    }
    catch (IOException ex)
    {
      Logger.getLogger(this.getClass()).error(ex.getMessage(), ex);
      throw new RuntimeException("Can't load setting file");
    }
  }

  public static Setting get()
  {
    return SettingHolder.instance;
  }

  public String getValue()
  {
    return this.properties.getProperty(this.toString());
  }
}
